import { getSessionItem, setSessionItem } from "@/basa/utils";
import {
  listPageOrder,
  removeOrder,
  enteringRevokeApproval
} from "../api/orderEntry";
import { formatNumber } from "../../../../basa/utils";

export default {
  data() {
    return {
      // 高级搜索相关
      isShowSearch: true,
      formDataSearch: {},
      formData: {
        bizStatus: "",
        pageNo: 1,
        pageSize: 10
      },
      // Tab切换
      isActive: 0,
      focuses: [
        { focus: "all-item" },
        { focus: "un-submitted" },
        { focus: "under-approval" },
        { focus: "return-approval" }
      ],
      tableData: [],
      tableColumnData: [
        {
          label: "序号",
          type: "index",
          fixed: true,
          width: "50"
        },
        {
          label: "订单编号",
          prop: "orderNo",
          width: "130",
          fixed: true
        },
        {
          label: "订单名称",
          prop: "orderName",
          width: "300"
        },
        {
          label: "分类名称",
          prop: "thirdCategoriesOfName",
          width: "130"
        },
        {
          label: "供应商",
          prop: "partyBName",
          width: "200"
        },
        {
          label: "订单数量",
          prop: "orderQuantity",
          width: "140"
        },
        {
          label: "订单发出时间",
          prop: "orderDeliveryTime",
          width: "100"
        },
        {
          label: "订单状态",
          prop: "bizStatus",
          width: "70"
        }
      ],
      // 表格数据处理
      formatterList: {},
      // 分页相关
      isShowPagination: false,
      totalNum: 0,
      pageNoCode: 1,
      pageSizeCode: this.getLocalPageSize(),
      tableName: this.tableList.orderEntry,
      localSearchData: {},
      bizStatusType: {
        2: "待提交",
        3: "审批中",
        4: "已撤回",
        5: "审批通过",
        6: "审批退回"
      },
      bizStatusSearch: [
        {
          label: "待提交",
          value: "2"
        },
        {
          label: "已撤回",
          value: "4"
        },
        {
          label: "审批中",
          value: "3"
        },
        {
          label: "审批退回",
          value: "6"
        },
        {
          label: "审批通过",
          value: "5"
        }
      ],
      buttonBool: false
    };
  },
  inject: ["reload"],
  methods: {
    handleTrim(key) {
      this.$set(
        this.formData,
        key,
        this.formData[key].replace(/(^\s*)|(\s*)$/g, "")
      );
    },
    // 列表数据获取
    getList(reqObj, status) {
      let obj = {};
      obj = typeof reqObj == "object" ? reqObj : obj;
      obj.pageNo = this.pageNoCode;
      obj.pageSize = this.pageSizeCode;
      obj.permissionsType = 1;
      // obj.isShowPageLoading = true;
      // obj.loadingMessage = "加载中,请稍等...";
      if (status === 0) {
        status = "";
      }
      obj.bizStatus = status;
      if (!obj.bizStatus) {
        delete obj.bizStatus;
        // 如果订单状态为全部，则将入参bizStatus替换成listQueryType = "1"
        obj.listQueryType = "1";
      }
      listPageOrder(obj).then(res => {
        this.isShowPagination = res.data.data.recordsTotal > 0;
        this.tableData = res.data.data.rows;
        this.tableData.map(e => {
          e.orderDeliveryTime = e.orderDeliveryTime
            ? e.orderDeliveryTime.substr(0, 10)
            : "";
          e.bizStatus = this.bizStatusType[e.bizStatus];
          e.orderQuantity = formatNumber(e.orderQuantity, 3);
        });
        this.totalNum = res.data.data.recordsTotal;
        this.pageNoCode = res.data.data.pageNo;
      });
    },
    // 提示弹窗
    deleteMessageBox(obj, params, status, callBack) {
      this.$confirm(obj.content, obj.title, {
        distinguishCancelAndClose: true,
        confirmButtonText: obj.confirmButtonText,
        cancelButtonText: "取消",
        customClass: "ccui-message-box-custom"
      })
        .then(() => {
          callBack(params, status);
        })
        .catch(e => {
        });
    },
    // 删除
    removeOrder(params, status) {
      removeOrder(params).then(res => {
        this.callBackThenMessage(res, status);
      });
    },
    // 撤回
    enteringRevokeApproval(params, status) {
      enteringRevokeApproval(params).then(res => {
        this.callBackThenMessage(res, status);
      });
    },
    // 删除 / 撤回成功提示message
    callBackThenMessage(res, status) {
      let { code, message } = res.data;
      if (code === "0") {
        this.$message.success(message);
      } else {
        this.$showErrorMessage(message);
      }
      // this.$message({
      //   message: message,
      //   type: code === "0" ? "success" : "error",
      //   duration: 2000,
      //   ccuiCustom: true
      // });
      this.getList({}, status);
    },
    // 高级搜索
    search(data, status) {
      this.pageNoCode = 1;
      this.formDataSearch = data;
      this.localSearchData = JSON.parse(JSON.stringify(data));
      setSessionItem("pageNo", this.tableName, {
        pageNo: this.pageNoCode,
        searchForm: this.localSearchData
      });
      if (this.formDataSearch.orderDeliveryTime) {
        this.formDataSearch.orderDeliveryTimeBegin = this.formData.orderDeliveryTime[0];
        this.formDataSearch.orderDeliveryTimeEnd =
          this.formDataSearch.orderDeliveryTime[1] + 86400000;
      }
      this.getList(this.formDataSearch, status);
    },
    // 高级搜索切换清空
    searchChange(val) {
      this.formData = {
        bizStatus: "",
        pageNo: 1,
        pageSize: 10
      };
    },
    // 分页相关功能
    // 展示列表条数更改
    handleSizeChange(val, id) {
      this.pageNoCode = 1;
      if (val !== "none") {
        this.pageSizeCode = val;
        setSessionItem("pageSize", this.tableName, val);
      }
      if (
        this.formDataSearch.bizStatus &&
        this.formDataSearch.bizStatus !== 0 &&
        id === 0
      ) {
        this.getList(this.formDataSearch, this.formDataSearch.bizStatus);
      } else {
        this.getList(this.formDataSearch, id);
      }
    },
    // 当前页码更改
    handleCurrentChange(val, id) {
      // let searchFormData = getSessionItem("pageNo", this.tableName) || {};
      // setSessionItem("pageNo", this.tableName, {
      //   pageNo: val,
      //   searchForm: searchFormData.searchForm || {}
      // });
      // this.formDataSearch = {
      //   ...this.formDataSearch,
      //   ...searchFormData.searchForm
      // };
      // 跳转翻页都会调这个方法

      this.pageNoCode = val;
      if (
        this.formDataSearch.bizStatus &&
        this.formDataSearch.bizStatus !== 0 &&
        id === 0
      ) {
        this.getList(this.formDataSearch, this.formDataSearch.bizStatus);
      } else {
        this.getList(this.formDataSearch, id);
      }
    },
    // 跳转详情
    handleGoDetail(r) {
      this.$router.push("/orderManagement/entry/detail/" + r.id);
    },
    // 跳转编辑
    handleGoEdit(r) {
      this.$router.push("/orderManagement/entry/edit/" + r.id);
    },
    // 跳转新增
    handleGoNew() {
      this.$router.push("/orderManagement/entry/add");
    },
    // Tab切换
    tabChange(data) {
      this.$router.push(`/orderManagement/entry/${data}`);
    },
    // 获取默认分页
    getLocalPageSize() {
      return getSessionItem("pageSize", this.tableList.orderEntry) || 10;
    },
    // 单元格点击跳转详情
    cellClick(r, column) {
      if (column.label === "订单编号") {
        this.$router.push("/orderManagement/entry/detail/" + r.id);
      }
    },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (column.label == "订单编号") {
        return { color: "#0089EF", cursor: "pointer" };
      }
    }
  },
  // beforeRouteLeave(to, from, next) {
  //   for (let key in this.$vnode.parent.componentInstance.cache) {
  //     delete this.$vnode.parent.componentInstance.cache[
  //         ishasAgentId(to.path, key)
  //         ];
  //   }
  //   next();
  // }
};
